package com.deyang.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.deyang.callback.RequestPermissionsCallBack;
import com.deyang.maillist.R;
import com.deyang.util.ImageUtil;
import com.deyang.util.Logger;
import com.deyang.util.PermissionUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class MailDetailActivity extends BaseActivity {


    @BindView(R.id.iv_userHeadIcon)
    ImageView ivUserHeadIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_duty)
    TextView tvDuty;
    @BindView(R.id.tv_phone)
    TextView tvPhone;


    private String mailPhone;

    @Override
    protected void initView(Bundle savedInstanceState) {
        //状态栏透明化
        setContentView(R.layout.activity_mail_detail);
        ButterKnife.bind(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String mailName = intent.getStringExtra("mailName");
        mailPhone = intent.getStringExtra("mailPhone");
        String mailDuty = intent.getStringExtra("mailDuty");
        String mailSex = intent.getStringExtra("mailSex");


        tvDuty.setText(mailDuty);
        tvName.setText(mailName);
        tvPhone.setText(mailPhone);
        if ("1".equals(mailSex)) {
            ImageUtil.loadImage(this, ivUserHeadIcon, R.mipmap.default_mail_man, R.drawable.load_fail);
        } else {
            ImageUtil.loadImage(this, ivUserHeadIcon, R.mipmap.default_mail_wuman, R.drawable.load_fail);
        }

    }

    @OnClick({
            R.id.ll_phone,
            R.id.ll_note,
    })
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_phone:
                callPhone();
                break;
            case R.id.ll_note:
                sendNote();
                break;
        }
    }

    /**
     * 打电话
     */
    private void callPhone() {
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.CALL_PHONE,
        };
        //存储权限
        boolean hasPermission = PermissionUtil.isHasPermission(this, Manifest.permission.CALL_PHONE);
        boolean hasLocationPermission = PermissionUtil.isHasPermission(this, Manifest.permission.CALL_PHONE);
        if (!hasPermission || !hasLocationPermission) {
            if (Build.VERSION.SDK_INT >= 23) {//判断当前系统是不是Android6.0
                requestPermissions(this, "申请获取拨打电话权限！", "拒绝此权限将影响部分功能使用"
                        , 0x001, new RequestPermissionsCallBack() {
                            @Override
                            public void onPermissionsGranted(List<String> perms) {
                                goCallPhone(mailPhone);
                            }
                        }, PERMISSIONS_STORAGE);
            } else {
                goCallPhone(mailPhone);
            }
        } else {
            goCallPhone(mailPhone);
        }
    }

    /**
     * 发短信
     */
    private void sendNote() {
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.SEND_SMS,
        };
        //存储权限
        boolean hasPermission = PermissionUtil.isHasPermission(this, Manifest.permission.SEND_SMS);
        boolean hasLocationPermission = PermissionUtil.isHasPermission(this, Manifest.permission.SEND_SMS);
        if (!hasPermission || !hasLocationPermission) {
            if (Build.VERSION.SDK_INT >= 23) {//判断当前系统是不是Android6.0
                requestPermissions(this, "申请获取发送短信权限！", "拒绝此权限将影响部分功能使用"
                        , 0x001, new RequestPermissionsCallBack() {
                            @Override
                            public void onPermissionsGranted(List<String> perms) {
                                doSendSMSTo(mailPhone, "");
                            }
                        }, PERMISSIONS_STORAGE);
            } else {
                doSendSMSTo(mailPhone, "");
            }
        } else {
            doSendSMSTo(mailPhone, "");
        }
    }

    private void goCallPhone(String mailPhone) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + mailPhone);
            intent.setData(data);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(TAG, e.getMessage());
            showToastError("当前设备不支持拨打电话！");
        }
    }


    /**
     * 调起系统发短信功能
     *
     * @param phoneNumber
     * @param message
     */
    public void doSendSMSTo(String phoneNumber, String message) {
        try {
            if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
                intent.putExtra("sms_body", message);
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e(TAG, e.getMessage());
            showToastError("当前设备不支持拨打电话！");
        }
    }
}
